package com.tkx.dxb.service.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tkx.dxb.service.article.entity.Comment;
import com.tkx.dxb.service.article.entity.vo.ApiCommentQueryVo;
import com.tkx.dxb.service.article.entity.vo.CommentQueryVo;
import com.tkx.dxb.service.article.mapper.CommentMapper;
import com.tkx.dxb.service.article.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author victor
 * @since 2021-04-07
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Override
    public IPage<Comment> selectPage(Page<Comment> pageParam, CommentQueryVo commentQueryVo) {
        /**
         * 显示分页查询列表
         * 1.排序：按照sort字段排序
         * 2.分页查询
         * 3.条件查询
         */
        //1
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("gmt_create");

        //2
        if (commentQueryVo == null){
            return baseMapper.selectPage(pageParam,queryWrapper);
        }

        //3
        String memberName = commentQueryVo.getMemberName();
        String begin = commentQueryVo.getCommentDateBegin();
        String end = commentQueryVo.getCommentDateEnd();

        if (!StringUtils.isEmpty(memberName)){
            //左%会使索引失效
            queryWrapper.likeRight("memberName", memberName);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create", end);
        }

        return baseMapper.selectPage(pageParam,queryWrapper);
    }

    @Override
    public IPage<Comment> selectApiPage(Page<Comment> pageParam, ApiCommentQueryVo apiCommentQueryVo) {
        /**
         * 显示分页查询列表
         * 1.排序：按照sort字段排序
         * 2.分页查询
         * 3.条件查询
         */
        //1
        String articleId = apiCommentQueryVo.getArticleId();
        String likeSort = apiCommentQueryVo.getLikeSort();
        String timeSort = apiCommentQueryVo.getTimeSort();

        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("article_id", articleId);

        if (!StringUtils.isEmpty(likeSort)){
            queryWrapper.orderByAsc("comment_like");
        }

        if (!StringUtils.isEmpty(timeSort)){
            queryWrapper.orderByAsc("gmt_create");
        }

        return baseMapper.selectPage(pageParam,queryWrapper);
    }
}
